package cn.qidd.keep.manage.controller.login;

import cn.qidd.keep.domain.core.Result;
import cn.qidd.keep.domain.document.User;
import cn.qidd.keep.domain.util.MD5;
import cn.qidd.keep.manage.model.login.LoginModel;
import cn.qidd.keep.manage.model.login.LoginParams;
import cn.qidd.keep.manage.repository.UserRepository;
import cn.qidd.keep.manage.service.login.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Objects;

@RestController
@RequestMapping("/api/login")
@Api(description = "登陆")
public class LoginController{

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    @ApiOperation(value = "登陆", notes = "登陆")
    public Result<LoginModel> login(@Validated @RequestBody LoginParams loginParams) {
        logger.debug("login:{}", loginParams);
        User user = userRepository.findByUserName(loginParams.getUserName());
        if (Objects.isNull(user)) {
            return new Result<>(Result.Status.FAILURE, "用户不存在");
        }
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        if (!passwordEncoder.matches(loginParams.getPassword(), user.getPassword())) {
            return new Result<>(Result.Status.FAILURE, "密码错误");
        }
        LoginModel loginModel = loginService.login(user);
        return new Result<>(Result.Status.SUCCESS, loginModel);
    }

    @GetMapping("/getUserInfoByToken")
    @ApiOperation(value = "根据token获取用户信息", notes = "根据token获取用户信息")
    public Result<LoginModel> getUserInfoByToken(@ApiIgnore @RequestHeader("X-Header-Token") String token) {
        logger.debug("getUserInfoByToken:{}", token);
        LoginModel loginModel = loginService.getUserInfoByToken(token);
        return new Result<>(Result.Status.SUCCESS, loginModel);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "退出登陆", notes = "退出登陆")
    public Result logout(@ApiIgnore @RequestHeader("X-UserToken") String token) {
        logger.debug("logout:{}", token);
        loginService.logout(token);
        return new Result(Result.Status.SUCCESS, "");
    }

    @PostMapping("/devLogin")
    @ApiOperation(value = "开发登陆", notes = "开发登陆")
    public Result<LoginModel> devLogin(@Validated @RequestBody LoginParams loginParams) {
        logger.debug("devLogin:{}", loginParams);
        User user = userRepository.findByUserName(loginParams.getUserName());
        if (Objects.isNull(user)) {
            return new Result<>(Result.Status.FAILURE, "用户不存在");
        }
        String md5EncodePassword = MD5.MD5Encode(loginParams.getPassword());
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        if (!passwordEncoder.matches(md5EncodePassword, user.getPassword())) {
            return new Result<>(Result.Status.FAILURE, "密码错误");
        }
        LoginModel loginModel = loginService.login(user);
        return new Result<>(Result.Status.SUCCESS, loginModel);
    }
}